package com.hsj.demo.service;

import com.hsj.demo.dao.*;
import com.hsj.demo.entity.Notice;
import com.hsj.demo.entity.User;
import com.hsj.demo.entity.UserFriend;
import com.hsj.demo.entity.UserNotice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserFriendService extends CurdService<UserFriendDao, UserFriend> {
    @Autowired
    private UserFriendDao userFriendDao;
    @Autowired
    private ChatHistoryDao chatHistoryDao;
    @Autowired
    private NoticeDao noticeDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserNoticeDao userNoticeDao;

    public Integer findUserFriendId(Integer sourceUserId,Integer targetUserId){
        return userFriendDao.findUserFriendId(sourceUserId,targetUserId);
    }

    public List<UserFriend> findBySourceId(Integer id,Character isRecently){
        List<UserFriend> userFriendList = userFriendDao.findBySourceId(id,isRecently);
        for(UserFriend userFriend:userFriendList){
            userFriend.setChatHistoryList(chatHistoryDao.findByTime(userFriend.getSourceUserId(),userFriend.getTargetUser().getId()));
        }
        return userFriendList;
    }

    public Notice insertFriend(UserFriend userFriend,User user) {
        userFriend.setSourceUserId(user.getId());
        if(userFriendDao.findUserFriendId(user.getId(),userFriend.getTargetUserId())!=null){
            return null;
        }
        Notice notice = new Notice();
        notice.setMessage(user.getUsername()+"请求添加您为好友");
        notice.setTargetType(2);
        notice.setSourceUserId(user.getId());
        notice.setTargetUserId(userFriend.getTargetUserId());
        notice.setSendTime(new Date());
        noticeDao.insert(notice);
        UserNotice userNotice = new UserNotice();
        userNotice.setUserId(notice.getTargetUserId());
        userNotice.setNoticeId(notice.getId());
        userNotice.setIsRead(0);
        userNoticeDao.insert(userNotice);
        userFriend.setIsAgree(0);
        userFriendDao.insert(userFriend);
        return notice;
    }

    public String agreeInsert(UserFriend userFriend) {
        userFriend.setIsAgree(1);
        userFriendDao.insert(userFriend);

        UserFriend userFriend1 = new UserFriend();
        userFriend1.setSourceUserId(userFriend.getTargetUserId());
        userFriend1.setTargetUserId(userFriend.getSourceUserId());

        userFriend1 = userFriendDao.findBySouAndTarId(userFriend1);
        userFriend1.setIsAgree(1);
        userFriend1.setIsRecently('0');
        userFriendDao.update(userFriend1);
        return "成功";
    }

    public String updateGroup(UserFriend userFriend,User user){
        userFriend.setSourceUserId(user.getId());
        userFriendDao.update(userFriend);
        return "修改成功";
    }

    public Notice deleteFriend(UserFriend userFriend,User user){
        userFriend.setSourceUserId(user.getId());
        // 管理员给被删除方，发送提醒
        Notice notice = new Notice();
        notice.setSourceUserId(userDao.findByUsername("admin").getId());
        notice.setTargetUserId(userFriend.getTargetUser().getId());
        notice.setTargetType(0);
        notice.setMessage("您已被"+user.getUsername()+"删除好友。");
        notice.setSendTime(new Date());
        //noticeDao.insert(notice);
        userFriendDao.deleteFriend(userFriend);
        return notice;
    }

    public List<UserFriend> findByKeyword(UserFriend userFriend,User user){
        userFriend.setSourceUserId(user.getId());
        return userFriendDao.findByEntity(userFriend);
    }

    public List<UserFriend> findChatHistory(Integer sourceUserId){
        System.out.println(sourceUserId);
        return userFriendDao.findChatHistory(sourceUserId);
    }
}
